/*

	This file prepares regressions sample for balancing regressions

*/

cd ${work}5_reg_analysis

use input\sample_it, clear

*OUTCOMES
*firm
preserve
	use input\firm_it, clear
	keep jurnr year employees sales profits investments equity assets nace4
	foreach v of varlist sales profits investments equity assets {
		replace `v'=`v'/1e3
	}
	deflate sales profits investments equity assets
	replace year=year+1
	save temp\temp, replace
restore
merge 1:1 jurnr year using temp\temp, keep(3) nogen
erase temp\temp.dta

*fire
preserve
	use input\fire_it, clear
	keep jurnr year capital wagebill expenditures purchase_buildings purchase_energy purchase_opeq rent inventory credit prepayments dividends
	foreach v of varlist capital wagebill expenditures purchase_buildings purchase_energy purchase_opeq rent inventory credit prepayments dividends {
		replace `v'=`v'/1e3
	}
	deflate capital wagebill expenditures purchase_buildings purchase_energy purchase_opeq rent inventory credit prepayments dividends
	replace year=year+1
	save temp\temp, replace
restore
merge 1:1 jurnr year using temp\temp, keep(3) nogen
erase temp\temp.dta

*export
preserve
	use input\export_raw_ipdt, clear
	collapse (sum) vrd_exp=vrd, by(jurnr year)
	replace vrd_exp=vrd_exp/1e3
	deflate vrd_exp
	replace year=year+1
	save temp\temp, replace
restore	
merge 1:1 jurnr year using temp\temp, keep(1 3) nogen
erase temp\temp.dta
replace vrd_exp=0 if vrd_exp==.	

*import
preserve
	use input\import_ipdt, clear
	collapse (sum) vrd_imp=vrd, by(jurnr year)
	replace vrd_imp=vrd_imp/1e3
	deflate vrd_imp
	replace year=year+1
	save temp\temp, replace
restore	
merge 1:1 jurnr year using temp\temp, keep(1 3) nogen
erase temp\temp.dta
replace vrd_imp=0 if vrd_imp==.	

*WINORIZE
winsor2 profits employees investments assets equity inventory purchase_buildings purchase_energy wagebill purchase_opeq credit dividends rent prepayments capital expenditures vrd_exp vrd_imp , cuts(1 99) replace

*LABEL
lab var profits "Profits"
lab var employees "Employees"
lab var investments "Investments"
lab var assets "Assets"
lab var equity "Equity"
lab var inventory "Inventory"
lab var purchase_buildings "Purchases, land and buildings"
lab var purchase_energy "Purchases, energy"
lab var wagebill "Wagebill"
lab var purchase_opeq "Purchases, operational equipment"
lab var credit "Receivables"
lab var dividends "Dividends" 
lab var rent "Rent"
lab var prepayments "Prepaid expenses"
lab var capital "Capital stock"
lab var expenditures "Purchases, raw materials"
lab var vrd_exp "Exports"
lab var vrd_imp "Imports"

*TEMP. SAVE
save temp\temp, replace

*EXTEND TO FIRM-DESTINATION-YEAR LEVEL
use input\sample_idt, clear
merge m:1 jurnr year using temp\temp, keep(3) nogen
erase temp\temp.dta

*MERGE ON EXPORT SUPPORT
destring jurnr, gen(jurnr_num)
merge 1:1 jurnr_num land year using input\promotion_idt, keep(1 3) keepusing(treated_1 tdcinitiative_1) nogen
drop jurnr_num
foreach v of varlist treated_1 tdcinitiative_1 {
	replace `v'=0 if `v'==.
}

*FEs
egen IDY=group(nace4 land year)
egen F=group(jurnr)

*SAVE
compress
save temp\balancesample_idt, replace